home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* dlist.c - uses the code in mountain.c to draw fractal-based
- * mountains MAX_FRAME times, first using display lists and then
- * in immediate mode. It prints the average frames/second for
- * both display list and immediate modes.
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/time.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( int );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid makeMountainLists( GLvoid );
- double getTime( void );
-
- void printHelp( char * );
-
- /* Functions in mountain.c */
-
- extern GLvoid setView( GLvoid );
- extern GLvoid computeMountain( GLvoid );
- extern GLvoid drawMountain( GLvoid );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define MAX_LIST 10
- #define MAX_FRAME 120
-
- /* Global Variables */
-
- static GLint mountainBase;
- static GLboolean displayListMode = GL_FALSE;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- printf( "\n%s contains a simple benchmarking loop "
- "that generates\nfractal-based mountains. "
- "It creates %d frames and displays them,\n"
- "first in immediate mode and then in display list mode,\n"
- "printing the average frames/second for each mode.\n\n",
- progname, MAX_FRAME );
- }
-
- GLvoid
- initgfx( void )
- {
- static GLfloat darkGreen[] = { 0.0, 0.5, 0.0, 1.0 };
-
- glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, darkGreen );
-
- glEnable( GL_LIGHTING );
- glEnable( GL_LIGHT0 );
-
- glClearColor( 0, 0, 0, 1 );
- glClear( GL_COLOR_BUFFER_BIT );
-
- setView();
-
- makeMountainLists();
-
- printf( "Starting Immediate Mode Benchmark\n");
- printf( "Immediate Mode results:\t\t");
- }
-
- /* makeMountainLists() creates MAX_LIST mountains and stores
- * each in a display list.
- */
- GLvoid
- makeMountainLists( GLvoid )
- {
- int mountain;
-
- /* set the seed so that we generate the same sequence of
- * mountains in both display list and immediate mode. */
- srand( 1 );
-
- printf( "Initializing %d Display Lists: ", MAX_LIST );
-
- mountainBase = glGenLists( MAX_LIST );
- for ( mountain = 0; mountain < MAX_LIST; mountain++ )
- {
- printf( "%d ", mountain );
- fflush( stdout );
-
- glNewList( mountainBase + mountain, GL_COMPILE );
- computeMountain();
- drawMountain();
- glEndList();
- }
-
- printf( "Done.\n\n" );
- }
-
- double
- getTime( void )
- {
- struct timeval tp;
- struct timezone tzp;
-
- gettimeofday(&tp,&tzp);
- return ((double)tp.tv_sec + (double)tp.tv_usec/1000000);
- }
-
- GLvoid
- keyboard( char key, int x, int y )
- {
- switch (key) {
- case KEY_ESC:
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 1.0, 350.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static int mountain = 0;
- static int frameCount = 0;
-
- static double t0;
- double elapsed;
-
- if ( frameCount == 0 )
- t0 = getTime();
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- if ( displayListMode )
- glCallList( mountainBase + mountain );
- else {
- computeMountain();
- drawMountain();
- }
-
- glutSwapBuffers();
-
- /* cycle through MAX_LIST different mountains */
- mountain = ++frameCount % MAX_LIST;
- if (mountain == 0) {
- /* reset the seed so that we generate the same sequence
- * of MAX_LIST mountains as those in the display lists */
- srand( 1 );
- }
-
- if (frameCount == MAX_FRAME ) {
- glFinish(); /* make sure everything is done */
-
- elapsed = getTime() - t0;
- printf( "Average %3.1lf frames/second\n\n",
- MAX_FRAME/elapsed );
-
- if ( displayListMode ) {
- exit(0);
- } else { /* switch to display list mode */
- displayListMode = GL_TRUE;
- frameCount = 0;
- printf( "Starting Display List Mode Benchmark\n" );
- printf( "Display List Mode results:\t" );
- }
- }
- }
-